package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpModifyPhoneDAO;
import org.jsola.hr.dto.EmpModifyPhoneAddDTO;
import org.jsola.hr.dto.EmpModifyPhoneUpdateDTO;
import org.jsola.hr.entity.EmpModifyPhoneDO;
import org.jsola.hr.query.EmpModifyPhoneQuery;
import org.jsola.hr.service.IEmpModifyPhoneService;
import org.jsola.hr.vo.EmpModifyPhoneListVO;
import org.jsola.hr.vo.EmpModifyPhoneVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工修改手机号
 *
 * @author zhr
 */
@Service("hrEmpModifyPhoneServiceImpl")
public class EmpModifyPhoneServiceImpl implements IEmpModifyPhoneService {

    @Autowired
    private IEmpModifyPhoneDAO empModifyPhoneDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpModifyPhoneVO save(EmpModifyPhoneAddDTO empModifyPhoneAddDTO, TokenUser tokenUser) {
        // 转DO
        EmpModifyPhoneDO empModifyPhoneDO = empModifyPhoneAddDTO.to(EmpModifyPhoneDO.class);
        // 保存
        empModifyPhoneDO = save(empModifyPhoneDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empModifyPhoneDO.to(EmpModifyPhoneVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpModifyPhoneUpdateDTO empModifyPhoneUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpModifyPhoneDO empModifyPhoneDO = empModifyPhoneUpdateDTO.to(EmpModifyPhoneDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empModifyPhoneDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empModifyPhoneIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empModifyPhoneIds);
    }


    @Override
    public EmpModifyPhoneVO selectById(String empModifyPhoneId, String siteId) {
        EmpModifyPhoneDO empModifyPhoneDO = selectDOById(empModifyPhoneId, siteId);
        if (empModifyPhoneDO == null) {
            return null;
        }
        return empModifyPhoneDO.to(EmpModifyPhoneVO.class);
    }

    @Override
    public List<EmpModifyPhoneListVO> select(EmpModifyPhoneQuery empModifyPhoneQuery, String siteId) {
        List<EmpModifyPhoneDO> empModifyPhoneDOList = selectDO(empModifyPhoneQuery, siteId);
        if(CollectionUtils.isEmpty(empModifyPhoneDOList)) {
            return empModifyPhoneDOList == null ? null : new ArrayList<>();
        }
        return empModifyPhoneDOList.stream()
                .map(empModifyPhoneDO -> empModifyPhoneDO.to(EmpModifyPhoneListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpModifyPhoneQuery empModifyPhoneQuery, String siteId) {
        Example example = buildExample(empModifyPhoneQuery,siteId);
        return empModifyPhoneDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpModifyPhoneListVO> selectPage(EmpModifyPhoneQuery empModifyPhoneQuery, String siteId) {
        Example example = buildExample(empModifyPhoneQuery,siteId);
        Page<EmpModifyPhoneDO> page = empModifyPhoneDAO.selectPageByExample(example,
                empModifyPhoneQuery.getPageNo(),
                empModifyPhoneQuery.getPageSize());

        return page.to(EmpModifyPhoneListVO.class);
    }

    @Override
    public EmpModifyPhoneDO selectDOById(String empModifyPhoneId, String siteId) {
        return listById(empModifyPhoneId, siteId);
    }

    @Override
    public List<EmpModifyPhoneDO> selectDO(EmpModifyPhoneQuery empModifyPhoneQuery, String siteId) {
        Example example = buildExample(empModifyPhoneQuery,siteId);
        return empModifyPhoneDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param empModifyPhoneQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpModifyPhoneQuery empModifyPhoneQuery, String siteId) {
        Example example = new Example(EmpModifyPhoneDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,empModifyPhoneQuery.getOrders());
        return example;
    }
}




